from PyQt6.QtWidgets import (QApplication, QMainWindow, 
                            QProgressBar, QPushButton, QVBoxLayout, QWidget)
from PyQt6.QtCore import QObject, pyqtSignal, QThread
import time

class Worker(QObject):
    progress = pyqtSignal(int)
    finished = pyqtSignal()
    
    def run(self):
        for i in range(101):
            time.sleep(0.05)  # 模拟耗时操作
            self.progress.emit(i)
        self.finished.emit()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("进度示例")
        self.setGeometry(100, 100, 300, 150)
        
        # 创建UI
        self.progress = QProgressBar()
        self.button = QPushButton("开始任务")
        
        layout = QVBoxLayout()
        layout.addWidget(self.progress)
        layout.addWidget(self.button)
        
        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)
        
        # 设置工作线程
        self.setup_worker()
        
        # 连接按钮
        self.button.clicked.connect(self.start_task)
    
    def setup_worker(self):
        self.thread = QThread()
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        
        # 连接信号
        self.worker.progress.connect(self.update_progress)
        self.worker.finished.connect(self.task_complete)
        self.thread.started.connect(self.worker.run)
    
    def start_task(self):
        self.button.setEnabled(False)
        self.thread.start()
    
    def update_progress(self, value):
        self.progress.setValue(value)
    
    def task_complete(self):
        self.button.setEnabled(True)
        self.thread.quit()

if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()
